﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindows.Commands;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Net.NetworkInformation;
using UIWindows.Tools;
using UIWindows.Models;
using UIWindows.Tools.Helper;

namespace UIWindows.ViewModels.DeviceConfig
{
    public class NICConfigurationVModel : Domains.ViewModelBase
    {
        public IPPortModel IPPort { get; set; }
        private List<NetworkInterface> _networkInterfaces = new List<NetworkInterface>();
        public ObservableCollection<string> NICCList { get; set; }
        public ObservableCollection<IPPortModel> IPPortList { get; set; }
        public NICConfigurationVModel()
        {
            _networkInterfaces.AddRange(NetworkAdapter.GetAllNetworkAdapters());
            NICCList = new ObservableCollection<string>(_networkInterfaces.Select(x => x.Name).ToList());
            IPPortList = new ObservableCollection<IPPortModel>(new IPPortModel[] { 
                new IPPortModel(){ 
                    IP = "192.168.1.200",
                    DNS = "255.255.255.0",
                },
                new IPPortModel(){
                    IP = "192.168.11.200",
                    DNS = "255.255.255.0",
                },
                new IPPortModel(){
                    IP = "192.168.16.200",
                    DNS = "255.255.255.0",
                },
                new IPPortModel(){
                    IP = "192.168.100.200",
                    DNS = "255.255.255.0",
                },
                new IPPortModel(){
                    IP = "192.168.250.200",
                    DNS = "255.255.255.0",
                }
            });
            IPPort = new IPPortModel() 
            { 
                IP = "192.168.10.200",
                DNS = "255.255.255.0",
            };
            if (_networkInterfaces.Count > 0)
            {
                NICC = _networkInterfaces[0].Name;
            }
        }

        private string _nicc;
        public string NICC
        {
            get { return _nicc; }
            set { 
                _nicc = value;
            }
        }


        public DelegateCommand<Button> Function => new DelegateCommand<Button>(sender =>
        {
            switch (sender.Content.ToString())
            {
                case "刷  新":
                    _networkInterfaces.Clear();
                    NICCList.Clear();
                    _networkInterfaces.AddRange(NetworkAdapter.GetAllNetworkAdapters());
                    foreach (var item in _networkInterfaces)
                    {
                        NICCList.Add(item.Name);
                    }
                    if (_networkInterfaces.Count > 0)
                    {
                        NICC = _networkInterfaces[0].Name;
                    }
                    break;
                case "设  置":
                    var model = _networkInterfaces.FirstOrDefault(x => x.Name == NICC);
                    if (model == null) { System.Windows.MessageBox.Show("找不到网卡");return; }
                    if (NetworkAdapter.SetIPAddress(IPPortList.Select(x => x.IP).ToArray(), IPPortList.Select(x => x.DNS).ToArray(), model.Id))
                    {
                        "设置成功".MessageDialogs(XingLucifer.IBase.Enums.MessageLevelType.成功, false, Enums.PopUpTypes.Global);
                    }
                    else
                    {
                        "设置失败".MessageDialogs(XingLucifer.IBase.Enums.MessageLevelType.错误, false, Enums.PopUpTypes.Global);
                    }
                    break;
            }
        }, Enums.AuthorityTypes.超级管理员_参照 | Enums.AuthorityTypes.管理员_参照);

        public DelegateCommand Add => new DelegateCommand(() =>
        {
            if (IPPortList.Any(x => x.IP == IPPort.IP && x.DNS == IPPort.DNS))
            {
                return;
            }
            IPPortList.Add(new IPPortModel()
            {
                IP = IPPort.IP,
                DNS = IPPort.DNS,
            });
        }, Enums.AuthorityTypes.超级管理员_参照 | Enums.AuthorityTypes.管理员_参照);
        public DelegateCommand Delete => new DelegateCommand(() =>
        {
            var model = IPPortList.FirstOrDefault(x => x.IP == IPPort.IP && x.DNS == IPPort.DNS);
            if (model != null)
            {
                IPPortList.Remove(model);
            }
        }, Enums.AuthorityTypes.超级管理员_参照 | Enums.AuthorityTypes.管理员_参照);

        public DelegateCommand<IPPortModel> MouseDoubleClick => new DelegateCommand<IPPortModel>(sender =>
        {
            if (sender != null)
            {
                IPPort.IP = sender.IP;
                IPPort.DNS = sender.DNS;
            }
        }, Enums.AuthorityTypes.超级管理员_参照 | Enums.AuthorityTypes.管理员_参照);
    }
}
